GtkTextView: Add a monospace property
authorMatthias Clasen <mclasen@redhat.com>
Tue, 7 Oct 2014 04:39:42 +0000 (00:39 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 7 Oct 2014 04:39:42 +0000 (00:39 -0400)
This is a convenient shortcut for a common case. It is implemented
by adding a .monospace style class to the text view, and letting
the theme decide about the monospace font to use.

docs/reference/gtk/gtk3-sections.txt
gtk/gtkstylecontext.h
gtk/gtktextview.c
gtk/gtktextview.h

index 81576cad072a2481ff268a661c40bdaff24b707f..6a1dacbf7c7af57d48e8dc66b8f6d91efa782b01 100644 (file)
@@ -3888,6 +3888,8 @@ gtk_text_view_set_input_purpose
 gtk_text_view_get_input_purpose
 gtk_text_view_set_input_hints
 gtk_text_view_get_input_hints
+gtk_text_view_set_monospace
+gtk_text_view_get_monospace
 GTK_TEXT_VIEW_PRIORITY_VALIDATE
 <SUBSECTION Standard>
 GTK_TEXT_VIEW
@@ -6011,6 +6013,7 @@ GTK_STYLE_CLASS_MENU
 GTK_STYLE_CLASS_MENUBAR
 GTK_STYLE_CLASS_MENUITEM
 GTK_STYLE_CLASS_MESSAGE_DIALOG
+GTK_STYLE_CLASS_MONOSPACE
 GTK_STYLE_CLASS_NEEDS_ATTENTION
 GTK_STYLE_CLASS_NOTEBOOK
 GTK_STYLE_CLASS_OSD
index b1e65dd22d193095bf667cf00ea01ec7ea6cd489..6e22a6cdcf1c0de35bae27f1300b3d3ec98bed24 100644 (file)
@@ -895,6 +895,16 @@ struct _GtkStyleContextClass
  */
 #define GTK_STYLE_CLASS_PAPER "paper"
 
+/**
+ * GTK_STYLE_CLASS_MONOSPACE:
+ *
+ * A CSS class that is added to text view that should use
+ * a monospace font.
+ *
+ * Since: 3.16
+ */
+#define GTK_STYLE_CLASS_MONOSPACE "monospace"
+
 /**
  * GTK_STYLE_REGION_ROW:
  *
index 15433366fab00a4dd6ba12cf34680301f3f07d70..0ba3c5b2fc6e2ddfe35c0144bf774b5580b1b931 100644 (file)
@@ -308,7 +308,8 @@ enum
   PROP_VSCROLL_POLICY,
   PROP_INPUT_PURPOSE,
   PROP_INPUT_HINTS,
-  PROP_POPULATE_ALL
+  PROP_POPULATE_ALL,
+  PROP_MONOSPACE
 };
 
 static GQuark quark_text_selection_data = 0;
@@ -905,6 +906,23 @@ gtk_text_view_class_init (GtkTextViewClass *klass)
                                                          FALSE,
                                                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
 
+  /**
+   * GtkTextview:monospace:
+   *
+   * If %TRUE, set the %GTK_STYLE_CLASS_MONOSPACE style class on the
+   * text view to indicate that a monospace font is desired.
+   *
+   * Since: 3.16
+   */
+  g_object_class_install_property (gobject_class,
+                                   PROP_MONOSPACE,
+                                   g_param_spec_boolean ("monospace",
+                                                         P_("Monospace"),
+                                                         P_("Whether to use a monospace font"),
+                                                         FALSE,
+                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
+
+  
 
    /* GtkScrollable interface */
    g_object_class_override_property (gobject_class, PROP_HADJUSTMENT,    "hadjustment");
@@ -3389,6 +3407,9 @@ gtk_text_view_set_property (GObject         *object,
           g_object_notify_by_pspec (object, pspec);
         }
       break;
+    case PROP_MONOSPACE:
+      gtk_text_view_set_monospace (text_view, g_value_get_boolean (value));
+      break;
 
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -3498,6 +3519,10 @@ gtk_text_view_get_property (GObject         *object,
       g_value_set_boolean (value, priv->populate_all);
       break;
 
+    case PROP_MONOSPACE:
+      g_value_set_boolean (value, gtk_text_view_get_monospace (text_view));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -10558,3 +10583,58 @@ gtk_text_view_get_input_hints (GtkTextView *text_view)
 
   return hints;
 }
+
+/**
+ * gtk_text_view_set_monospace:
+ * @text_view: a #GtkTextView
+ * @monospace: %TRUE to request monospace styling
+ *
+ * Sets the #GtkTextView:monospace property, which
+ * indicates that the text view should use monospace
+ * fonts.
+ *
+ * Since: 3.16
+ */
+void
+gtk_text_view_set_monospace (GtkTextView *text_view,
+                             gboolean     monospace)
+{
+  GtkStyleContext *context;
+  gboolean has_monospace;
+
+  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
+
+  context = gtk_widget_get_style_context (GTK_WIDGET (text_view));  
+  has_monospace = gtk_style_context_has_class (context, GTK_STYLE_CLASS_MONOSPACE);
+
+  if (has_monospace != monospace)
+    {
+      if (monospace)
+        gtk_style_context_add_class (context, GTK_STYLE_CLASS_MONOSPACE);
+      else
+        gtk_style_context_remove_class (context, GTK_STYLE_CLASS_MONOSPACE);
+      g_object_notify (G_OBJECT (text_view), "monospace");
+    }
+}
+
+/**
+ * gtk_text_view_get_monospace:
+ * @text_view: a #GtkTextView
+ *
+ * Gets the value of the #GtkTextView:monospace property.
+ *
+ * Return: %TRUE if monospace fonts are desired
+ *
+ * Since: 3.16
+ */
+gboolean
+gtk_text_view_get_monospace (GtkTextView *text_view)
+{
+  GtkStyleContext *context;
+
+  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
+
+  context = gtk_widget_get_style_context (GTK_WIDGET (text_view));
+  
+  return gtk_style_context_has_class (context, GTK_STYLE_CLASS_MONOSPACE);
+}
index 23ff349e9280f45ad3245fea36dfb190d5f2c1fa..df1f5d948322356585292353073cf888254869c4 100644 (file)
@@ -408,6 +408,11 @@ void             gtk_text_view_set_input_hints        (GtkTextView      *text_vi
 GDK_AVAILABLE_IN_3_6
 GtkInputHints    gtk_text_view_get_input_hints        (GtkTextView      *text_view);
 
+GDK_AVAILABLE_IN_3_16
+void             gtk_text_view_set_monospace          (GtkTextView      *text_view,
+                                                       gboolean          monospace);
+GDK_AVAILABLE_IN_3_16
+gboolean         gtk_text_view_get_monospace          (GtkTextView      *text_view);
 
 G_END_DECLS